package com.fingard.dsp.bank.directbank.lzcb01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.reqrespobj.single.*;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.net.ConnState;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;
import org.apache.commons.lang.StringUtils;

import java.util.Date;

public class LZCB01SinglePay extends LZCB01Base implements ISinglePay {
    @Override
    public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
        RetAtsSingle retAts = new RetAtsSingle(p_req1908.reqHeader);
        RetAtsSglDetail retDetail = new RetAtsSglDetail();
        try {
            LZCB01Header bankHeader = new LZCB01Header();
            XmlTextWriter xmlWriter = new XmlTextWriter();
            //同行转账
            if(p_req1908.singleDetail.sameBnk.equals("1")){
                bankHeader.transCode = "B2ECSTZBankInnerTransfer";
                bankHeader.reqSeqNo = p_req1908.reqHeader.transSeqID;

                writeCommonHeader(xmlWriter, bankHeader);
                xmlWriter.writeStartElement("Body");
                xmlWriter.writeElementString("ERPJnlNo", p_req1908.singleDetail.askNumber);
                xmlWriter.writeElementString("PayerAcNo", p_req1908.singleDetail.payAct);
                xmlWriter.writeElementString("PayerCurrency", p_req1908.singleDetail.payCur);
                xmlWriter.writeElementString("PayerAcName", p_req1908.singleDetail.payName);
                xmlWriter.writeElementString("PayeeAcNo", p_req1908.singleDetail.recAct);
                xmlWriter.writeElementString("PayeeAcName", p_req1908.singleDetail.recName);
                xmlWriter.writeElementString("Amount", p_req1908.singleDetail.payAmount);
                xmlWriter.writeElementString("Remark", p_req1908.singleDetail.memo);
                xmlWriter.writeElementString("TransTime",Format.Date8Format.format(new Date()));
                xmlWriter.writeEndElement();
                xmlWriter.writeEndDocument();
            }else{
                bankHeader.transCode = "B2ECSTZCrossBankTransfer";
                bankHeader.reqSeqNo = p_req1908.reqHeader.transSeqID;

                writeCommonHeader(xmlWriter, bankHeader);
                xmlWriter.writeStartElement("Body");
                xmlWriter.writeElementString("ERPJnlNo", p_req1908.singleDetail.askNumber);
                xmlWriter.writeElementString("PayerAcNo", p_req1908.singleDetail.payAct);
                xmlWriter.writeElementString("PayerAcName", p_req1908.singleDetail.payName);
                xmlWriter.writeElementString("PayeeAcNo", p_req1908.singleDetail.recAct);
                xmlWriter.writeElementString("PayeeAcName", p_req1908.singleDetail.recName);
                xmlWriter.writeElementString("UnionDeptId", p_req1908.singleDetail.cnapsCode);
                xmlWriter.writeElementString("PayerCurrency", p_req1908.singleDetail.payCur);
                xmlWriter.writeElementString("Amount", p_req1908.singleDetail.payAmount);
                xmlWriter.writeElementString("Remark", p_req1908.singleDetail.memo);
                xmlWriter.writeElementString("TransTime",Format.Date8Format.format(new Date()));
                xmlWriter.writeEndElement();
                xmlWriter.writeEndDocument();
            }
            //发送请求报文，并获取响应报文
            String[] retStrArr = sendToBank(xmlWriter.toXmlString(),bankHeader);
            //解析响应报文

            if (retStrArr[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(retStrArr[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRetCode = tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");
                    String tmpRetInfo = tmpBkReader.getFirstText("Message/Head/IBSReturnMsg");
                    if ("000000".equals(tmpRetCode)) {
                        String repCode = tmpBkReader.getFirstText("/Message/Body/ReturnCode");
                        String repInfo = tmpBkReader.getFirstText("Message/Body/ReturnMsg");
                        if(repCode!=null && repCode.equals("000000")) {
                            retDetail.respCode = "0";
                            retDetail.respInfo = "交易处理中";
                            retDetail.reqSeqID = tmpBkReader.getFirstText("/Message/Body/UnionDeptId");
                            retDetail.transState = p_req1908.reqHeader.commandCode + "+" + tmpBkReader.getFirstText("/Message/Body/ReturnCode");
                            retDetail.payInfoCode = retDetail.transState;
                        }else{
                            retDetail.respCode = "-1";
                            retDetail.respInfo = repInfo;
                        }
                    } else {
                        retDetail.respCode = "-1";
                        retDetail.respInfo = tmpRetInfo;
                    }
                } else {
                    retDetail.respCode = "-1";
                    retDetail.respInfo = tmpSbMsg.toString();
                    retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
                }
            } else if (ConnState.FAIL.equals(retStrArr[0])) {
                retDetail.respCode = "0";
                retDetail.respInfo = retStrArr[1];
                retDetail.transState = PaymentState.FAIL_Payment;
                retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = retStrArr[1];
                retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
            }
        } catch (Exception e) {
            retDetail = new RetAtsSglDetail();
            retDetail.respCode = "-1";
            retDetail.respInfo = e.getMessage();
            e.printStackTrace();
        }
        retAts.writeRetDetail(retDetail);
        return retAts;
    }

    @Override
    public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
        return null;
    }

    @Override
    public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {

        RetAtsSingle retAts = new RetAtsSingle(p_req1944.reqHeader);
        RetAtsSglDetail tmpRetAtsDetail = new RetAtsSglDetail();

        try {

            //region 请求报文封装
            LZCB01Header bankHeader = new LZCB01Header();
            bankHeader.transCode = "B2ECSTZTrsResultQry";
            bankHeader.reqSeqNo = p_req1944.reqHeader.transSeqID;

            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            writeCommonHeader(xmlWriter, bankHeader);//写入通用的头部节点
            xmlWriter.writeStartElement("Body");
            xmlWriter.writeElementString("HistoryERPJnlNo", p_req1944.askNumber);
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();
            //发送请求报文，并获取响应报文
            String[] retStrArr = sendToBank(xmlWriter.toXmlString(),bankHeader);
            if (retStrArr[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(retStrArr[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRetCode = tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");
                    String tmpRetInfo = tmpBkReader.getFirstText("Message/Head/IBSReturnMsg");
                    if ("000000".equals(tmpRetCode)) {
                        String repCode = tmpBkReader.getFirstText("/Message/Body/TrsStateCode");
                        String repInfo = tmpBkReader.getFirstText("Message/Body/TrsStateMsg");
                        String JnlState = tmpBkReader.getFirstText("Message/Body/JnlState");
                        if(repCode!=null && repCode.equals("000000")) {
                            tmpRetAtsDetail.reqSeqID = p_req1944.askNumber;
                            tmpRetAtsDetail.respCode = "0";
                            tmpRetAtsDetail.respInfo = "交易成功";
                            tmpRetAtsDetail.transState = p_req1944.reqHeader.commandCode + "+" + repCode;
                            tmpRetAtsDetail.payInfoCode = repInfo;
                        }else if(JnlState!=null && JnlState.equals("0")){
                            tmpRetAtsDetail.reqSeqID = p_req1944.askNumber;
                            tmpRetAtsDetail.respCode = "0";
                            tmpRetAtsDetail.respInfo = "交易成功";
                            tmpRetAtsDetail.transState = p_req1944.reqHeader.commandCode + "+" + "000000";
                            tmpRetAtsDetail.payInfoCode = repInfo;
                        }else if(JnlState!=null && JnlState.equals("1")){
                            tmpRetAtsDetail.reqSeqID = p_req1944.askNumber;
                            tmpRetAtsDetail.respCode = "-1";
                            tmpRetAtsDetail.respInfo = "通讯失败";
                            tmpRetAtsDetail.transState = p_req1944.reqHeader.commandCode + "+" + JnlState;
                            tmpRetAtsDetail.payInfoCode = repInfo;
                        }else if(JnlState!=null && JnlState.equals("2")){
                            tmpRetAtsDetail.reqSeqID = p_req1944.askNumber;
                            tmpRetAtsDetail.respCode = "-1";
                            tmpRetAtsDetail.respInfo = "主机拒绝";
                            tmpRetAtsDetail.transState = p_req1944.reqHeader.commandCode + "+" + JnlState;
                            tmpRetAtsDetail.payInfoCode = repInfo;
                        }else if(JnlState!=null && JnlState.equals("3")){
                            tmpRetAtsDetail.reqSeqID = p_req1944.askNumber;
                            tmpRetAtsDetail.respCode = "-1";
                            tmpRetAtsDetail.respInfo = "网银异常";
                            tmpRetAtsDetail.transState = p_req1944.reqHeader.commandCode + "+" + JnlState;
                            tmpRetAtsDetail.payInfoCode = repInfo;
                        }else if(JnlState!=null && JnlState.equals("4")){
                            tmpRetAtsDetail.reqSeqID = p_req1944.askNumber;
                            tmpRetAtsDetail.respCode = "-1";
                            tmpRetAtsDetail.respInfo = "交易冲正";
                            tmpRetAtsDetail.transState = p_req1944.reqHeader.commandCode + "+" + JnlState;
                            tmpRetAtsDetail.payInfoCode = repInfo;
                        }else if(JnlState!=null && JnlState.equals("5")){
                            tmpRetAtsDetail.reqSeqID = p_req1944.askNumber;
                            tmpRetAtsDetail.respCode = "-1";
                            tmpRetAtsDetail.respInfo = "交易未发生（主机并未接收到交易请求）";
                            tmpRetAtsDetail.transState = p_req1944.reqHeader.commandCode + "+" + JnlState;
                            tmpRetAtsDetail.payInfoCode = repInfo;
                        }else if(JnlState!=null && JnlState.equals("9")){
                            tmpRetAtsDetail.reqSeqID = p_req1944.askNumber;
                            tmpRetAtsDetail.respCode = "-1";
                            tmpRetAtsDetail.respInfo = "初始状态";
                            tmpRetAtsDetail.transState = p_req1944.reqHeader.commandCode + "+" + JnlState;
                            tmpRetAtsDetail.payInfoCode = repInfo;
                        }
                    }else{
                        tmpRetAtsDetail.respCode = "-1";
                        tmpRetAtsDetail.respInfo = tmpRetInfo;
                    }
                } else {
                    tmpRetAtsDetail.respCode = "-1";
                    tmpRetAtsDetail.respInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRetAtsDetail.respCode = "-1";
                tmpRetAtsDetail.respInfo = retStrArr[1];
            }
        } catch (Exception e) {
            tmpRetAtsDetail.respCode = "-1";
            tmpRetAtsDetail.respInfo = e.getMessage();
            e.printStackTrace();
        }

        retAts.writeRetDetail(tmpRetAtsDetail);
        return retAts;
    }

    @Override
    public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
        return null;
    }
}
